#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: sync_source_node.py 
@version:
@time: 2022/01/06 
"""
from TapClientCore.T04_logic.Dag01.node.node_base import NodeBase, TapdataNoThisDataSource


class CommonSyncSourceNode(NodeBase):

    def __init__(self, name, data):
        self.id = self.newId
        if isinstance(name, dict):
            self.name = name['name']
        else:
            self.name = name
        self.type = data['type']
        self.table = data['table']
        self.attribute = self.sole()
        self.tableId = None

    def sole(self):
        response = self.TM.get_connections_by_name(self.name)
        payload = {
            'where': {
                "source.id": response["data"][0]["id"],
                "meta_type": {"in": ["collection", "table", "view"]},
                "is_deleted": False
            },
            "fields": {"id": True, "original_name": True}
        }
        response2 = self.TM.get_metadataInstances_by_filter(payload)
        for i in response2["data"]:
            if i["original_name"] == self.table:
                self.tableId = i["id"]
                break
        if self.tableId is None:
            print("Find Table ID ERROR")
            assert False
        this_node = {
            'id': self.id,
            'name': self.name,
            "inputLanes": [],
            "outputLanes": [],
            "connectionId": response["data"][0]["id"],
            "databaseType": response["data"][0]["database_type"],
            "tableName": self.table,
            "tableId": self.tableId,
            "type": self.type
        }
        return this_node
